home *** CD-ROM | disk | FTP | other *** search
- Path: ssds.com!usenet
- From: Ron Romero <ron.romero@ssds.com>
- Newsgroups: comp.lang.c++
- Subject: Re: file processing...
- Date: Thu, 21 Mar 1996 13:37:30 -0500
- Organization: SSDS, Inc.
- Distribution: inet
- Message-ID: <3151A1EA.647C@ssds.com>
- References: <4iqr98$i3p@netnews.upenn.edu>
- NNTP-Posting-Host: rem_vie26.ssds.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win16; I)
- CC: ron.romero@ssds.com
-
- Sonny wrote:
- >
- > I want to read the 1st line of a text file into an array. The only way I know of to
- > detect the end of a line is by checking for "\n", but the code I wrote does not seem
- > be detecting it. Here is the code...
- >
- > #include<fstream.h>
- > #include<iostream.h>
- > #include<String.h>
- >
- > int readf(char array[],char *);
- > int readf (char array[], char *filename)
- > {
- > int i=0;
- > char c;
- >
- > ifstream inFile(filename, ios::in);
- > while (inFile >> c)
- > if (c!="\n") /*This line is giving me problem. It doesn't even compile */
- > array[i++]=c;
- > else
- > break;
- > return i;
- >
- > }
- >
- > [main deleted]
-
- The short answer is that you're comparing a character to a string, so you're
- getting a type mismatch error. Double quotes mark a string; single quotes mark
- a character. So the line that's giving you trouble should be
- if (c!='\n')
-
- The long answer is that you should be using istream::getline. This would make
- your main look like:
-
- void main(int argc,char *argv[])
- {
- int i;
- char array[50];
- ifstream inFile("gro", ios::in);
- inFile.getline(array, 50, '\n');
-
- for(int c=0; array[c] != '\0'; c++)
- cout<< array[c];
- cout << "\n";
- }
-
- Hope this helps.
-
- --------------------
- Ron Romero
- ron.romero@ssds.com
-